package com.yisu.expressway.ewf.common.ali;

import com.google.gson.Gson;
import com.yisu.expressway.ewf.common.ali.entity.PeccancyResponse;
import com.yisu.expressway.ewf.common.enums.ResponseCode;
import com.yisu.expressway.ewf.common.exception.StageException;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.Map;

public class AliApiUtil {

	public static PeccancyResponse post(String apiUrl, String appkey, String appSecret, Map<String, String> params)  {
		ShowapiRequest request = new ShowapiRequest(appkey, appSecret, apiUrl);
		if(!CollectionUtils.isEmpty(params)) {
			for (Map.Entry<String, String> entry : params.entrySet()) {
				request.addTextPara(entry.getKey(), entry.getValue());
			}
		}
		try {
			String result = new String(request.postAsByte(), "utf-8");
			return new Gson().fromJson(result, PeccancyResponse.class);
		} catch (Exception e) {
			throw new StageException(ResponseCode.FAILURE.code, ResponseCode.FAILURE.msg, e);
		}
	}

	public static void main(String[] args){
		Map<String, String> params = new HashMap<>();
		params.put("carCode", "004992");
		params.put("carEngineCode", "lj1963");
		params.put("carNumber","浙aaj601");
		params.put("carType", "02");

		PeccancyResponse result = post("http://ali-carlaw.showapi.com/break-rules", "23490062", "c91be4eeadeb957b8972b0051e6164b0", params);
		System.out.println(result);
	}
}